﻿<navigation:Page x:Class="SampleBrowser.Views.SampleFullTextSearch" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           xmlns:doc="http://schemas.firstfloorsoftware.com/documenttoolkit"
           xmlns:dte="http://schemas.firstfloorsoftware.com/documenttoolkit/extensions"
           xmlns:behaviors="clr-namespace:SampleBrowser.Behaviors"
           xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Full Text Search">
    <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="300" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <TextBlock x:Name="HeaderText" Grid.ColumnSpan="2" Style="{StaticResource HeaderTextStyle}" Text="Full Text Search"/>

        <doc:DocumentDataSource x:Name="DataSource" LoadError="DataSource_LoadError" SearchCompleted="DataSource_SearchCompleted"/>

        <doc:DocumentViewer x:Name="Viewer"
                            Grid.Row="1"
                            DocumentDataSource="{Binding ElementName=DataSource}">

            <!-- enable text selection to display search results-->
            <doc:DocumentViewer.Selection>
                <doc:TextSelection IsEnabled="True" />
            </doc:DocumentViewer.Selection>
            
            <!-- set view mode to fit width -->
            <doc:DocumentViewer.ViewMode>
                <doc:ViewMode ScaleMode="FitWidth" />
            </doc:DocumentViewer.ViewMode>
        </doc:DocumentViewer>

        <Grid Grid.Row="1" Grid.Column="1" Margin="4,0,4,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock FontWeight="Bold">
                <Run Text="Found" />
                <Run Text="{Binding SearchResults.Count, ElementName=DataSource}" />
                <Run Text="match(es)" />
            </TextBlock>
            <ListBox x:Name="SearchResults" Grid.Row="1" ItemsSource="{Binding SearchResults, ElementName=DataSource}" SelectionChanged="SearchResults_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock>
                        <Run Text="Page:" />
                        <Run Text="{Binding Range.Start.PageNumber}" />
                        <Run Text=", char:" />
                        <Run Text="{Binding Range.Start.TextIndex}" />
                        <Run Text="," />
                        <Run Text="{Binding Value}" />
                        </TextBlock>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

        <StackPanel Grid.ColumnSpan="2" Grid.Row="2" Orientation="Horizontal" Margin="0,8,0,0" HorizontalAlignment="Center">
            <doc:PageNavigator x:Name="Navigator"
                               PageCount="{Binding PageCount, ElementName=Viewer}"
                               PageIndex="{Binding PageIndex, ElementName=Viewer, Mode=TwoWay}"
                               Margin="4,0,0,0" />

            <Button Margin="4,0,0,0" Padding="7">
                <Button.Command>
                    <dte:OpenPdfOrXpsFromDiskCommand DocumentDataSource="{Binding ElementName=DataSource}" />
                </Button.Command>
                <Button.Content>
                    <Image Source="/SampleBrowser;component/Assets/Icons/open.png" />
                </Button.Content>
            </Button>

            <TextBox x:Name="SearchText" Width="150" Margin="4,0,0,0">
                <i:Interaction.Behaviors>
                    <behaviors:TextBoxEnterBehavior Command="{Binding Command, ElementName=Search}" CommandParameter="{Binding CommandParameter, ElementName=Search}" />
                </i:Interaction.Behaviors>
            </TextBox>
            <Button x:Name="Search" Content="Search" Margin="4,0,0,0" Command="{Binding SearchCommand, ElementName=DataSource}">
                <Button.CommandParameter>
                    <doc:SearchQuery TextToSearch="{Binding Text, ElementName=SearchText}">
                        <!-- using the built-in regex search provider -->
                        <doc:SearchQuery.SearchProvider>
                            <doc:RegexSearchProvider Options="IgnoreCase" />
                        </doc:SearchQuery.SearchProvider>
                    </doc:SearchQuery>
                </Button.CommandParameter>
            </Button>
            <Button Content="Cancel search" Margin="4,0,0,0" Command="{Binding CancelSearchCommand, ElementName=DataSource}" />
        </StackPanel>
    </Grid>
</navigation:Page>
